package com.rc.fragment;

import java.util.ArrayList;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rc.activity.FeedBack;
import com.rc.activity.R;
import com.rc.activity.ReceiptBox;
import com.rc.activity.ReceiptBox.OnBackPressedListener;
import com.rc.activity.Settings;
import com.rc.activity.YourProfile;
import com.rc.activity.dao.tbl_Count;
import com.rc.adapter.ListMenuAdapter;
import com.rc.custom.MyListView;
import com.rc.db.DatabaseSupport;
import com.rc.util.Util;
import com.telpoo.frame.model.BaseModel;
import com.telpoo.frame.ui.BaseFragment;

public class MenuFragment extends BaseFragment implements OnClickListener,
		OnBackPressedListener {
	public static View vMenu;
	public ListMenuAdapter adpReceiptType, adpReportType, adpTripType;
	private ReceiptBox receiptBox;
	public MyListView lvReceiptType, lvReportType, lvTripType;
	public ArrayList<tbl_Count> arrReceiptType = null, arrReportType = null,
			arrTripType = null;
	private LinearLayout llYourProfile, llSettings, llFeedback;
	private ReceiptUpdateBroadcast receiptBroadcast;
	private ReportUpdateBroadcast reportBroadcast;
	private TripUpdateBroadcast tripBroadcast;
	private Boolean myReceiptReceiverIsRegistered = false,
			myReportReceiverIsRegistered = false,
			myTripReceiverIsRegistered = false;

	public MenuFragment() {
		super();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		// register
		vMenu = (ViewGroup) inflater.inflate(R.layout.main_menu, null);
		llYourProfile = (LinearLayout) vMenu.findViewById(R.id.llYourProfile);
		llSettings = (LinearLayout) vMenu.findViewById(R.id.llSettings);
		llFeedback = (LinearLayout) vMenu.findViewById(R.id.llFeedback);
		lvReceiptType = (MyListView) vMenu.findViewById(R.id.lvReceiptType);
		lvReportType = (MyListView) vMenu.findViewById(R.id.lvReportType);
		lvTripType = (MyListView) vMenu.findViewById(R.id.lvTripType);
		// Funtion
		llYourProfile.setOnClickListener(this);
		llSettings.setOnClickListener(this);
		llFeedback.setOnClickListener(this);
		receiptBox = (ReceiptBox) getActivity();
		// register broadcast
		receiptBroadcast = new ReceiptUpdateBroadcast();
		reportBroadcast = new ReportUpdateBroadcast();
		tripBroadcast = new TripUpdateBroadcast();

		return vMenu;
	}

	@Override
	public void onResume() {
		super.onResume();
		if (!myReceiptReceiverIsRegistered) {
			getActivity().registerReceiver(receiptBroadcast,
					new IntentFilter("update.receipt"));
			myReceiptReceiverIsRegistered = true;
		}
		if (!myReportReceiverIsRegistered) {
			getActivity().registerReceiver(reportBroadcast,
					new IntentFilter("update.report"));
			myReportReceiverIsRegistered = true;
		}
		if (!myTripReceiverIsRegistered) {
			getActivity().registerReceiver(tripBroadcast,
					new IntentFilter("update.trip"));
			myTripReceiverIsRegistered = true;
		}
		// ReceiptType
		Util.HidenKeyBoard(getActivity());
		arrReceiptType = new ArrayList<tbl_Count>();
		arrReceiptType = DatabaseSupport.getArrReceiptTypes(getResources(),
				getActivity());

		adpReceiptType = new ListMenuAdapter(getActivity(), arrReceiptType);
		lvReceiptType.setAdapter(adpReceiptType);
		lvReceiptType.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				receiptBox.setBackground(R.color.background_title_receipt,
						getResources());
				Fragment mContent = new ReceiptFragment(Util
						.getReceiptTypeByText(getActivity(),
								((TextView) lvReceiptType.getChildAt(arg2)
										.findViewById(R.id.txtText)).getText()
										.toString()), 0, 0);
				if (mContent != null) {
					Util.pushFragmentReceiptBox(mContent,
							(ReceiptBox) getActivity(), "1");
					((ReceiptBox) getActivity()).toggle();
				}
			}
		});

		// ReceiptType
		arrReportType = new ArrayList<tbl_Count>();
		arrReportType = DatabaseSupport.getArrReportTypes(getResources(),
				getActivity());

		adpReportType = new ListMenuAdapter(getActivity(), arrReportType);
		lvReportType.setAdapter(adpReportType);
		lvReportType.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				receiptBox.setBackground(R.color.background_title_reports,
						getResources());
				Fragment mContent = new ReportFragment(arrReportType.get(arg2)
						.getName(), 0, 0);
				if (mContent != null) {
					Util.pushFragmentReceiptBox(mContent,
							(ReceiptBox) getActivity(), "1");
					((ReceiptBox) getActivity()).toggle();
				}
			}
		});

		// ReceiptType
		arrTripType = new ArrayList<tbl_Count>();
		arrTripType = DatabaseSupport.getArrTripTypes(getResources(),
				getActivity());

		adpTripType = new ListMenuAdapter(getActivity(), arrTripType);
		lvTripType.setAdapter(adpTripType);
		lvTripType.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				receiptBox.setBackground(R.color.background_title_trip,
						getResources());
				Fragment mContent = new TripFragment(((TextView) lvTripType
						.getChildAt(arg2).findViewById(R.id.txtText)).getText()
						.toString(), 0, 0);
				if (mContent != null) {
					Util.pushFragmentReceiptBox(mContent,
							(ReceiptBox) getActivity(), "1");
					((ReceiptBox) getActivity()).toggle();
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.llYourProfile:
			// TO DO input UserID
			YourProfile.callIntent(getActivity());
			break;
		case R.id.llSettings:
			Settings.callIntent(getActivity());
			break;
		case R.id.llFeedback:
			FeedBack.callIntent(getActivity());
			break;
		default:
			break;
		}
	}

	@Override
	public void doBack(Activity at) {
		// TODO Auto-generated method stub
		at.finish();
	}

	@Override
	public void onPause() {
		super.onPause();
		if (myReceiptReceiverIsRegistered) {
			getActivity().unregisterReceiver(receiptBroadcast);
			myReceiptReceiverIsRegistered = false;
		}
		if (myReportReceiverIsRegistered) {
			getActivity().unregisterReceiver(reportBroadcast);
			myReportReceiverIsRegistered = false;
		}
		if (myTripReceiverIsRegistered) {
			getActivity().unregisterReceiver(tripBroadcast);
			myTripReceiverIsRegistered = false;
		}
	}

	// Get BaseModel
	protected BaseModel getModel() {
		return receiptBox.model;
	}

	public void resetMenuList(Resources resousce, Context ct) {
		arrReceiptType = DatabaseSupport.getArrReceiptTypes(resousce, ct);
		if (adpReceiptType == null) {
			adpReceiptType = new ListMenuAdapter(ct, arrReceiptType);
			lvReceiptType = (MyListView) vMenu.findViewById(R.id.lvReceiptType);
			lvReceiptType.setAdapter(adpReceiptType);
		} else {
			adpReceiptType.NotiDB(arrReceiptType);
		}
		arrReportType = DatabaseSupport.getArrReportTypes(resousce, ct);
		if (adpReportType == null) {
			adpReportType = new ListMenuAdapter(ct, arrReportType);
			lvReportType = (MyListView) vMenu.findViewById(R.id.lvReportType);
			lvReportType.setAdapter(adpReportType);
		} else {
			adpReportType.NotiDB(arrReportType);
		}
		arrTripType = DatabaseSupport.getArrTripTypes(resousce, ct);
		if (adpTripType == null) {
			adpTripType = new ListMenuAdapter(ct, arrTripType);
			lvTripType = (MyListView) vMenu.findViewById(R.id.lvTripType);
			lvTripType.setAdapter(adpTripType);
		} else {
			adpTripType.NotiDB(arrTripType);
		}
	}

	private class ReceiptUpdateBroadcast extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			Log.e("push-update-receipt", "1");
			arrReceiptType = new ArrayList<tbl_Count>();
			arrReceiptType = DatabaseSupport.getArrReceiptTypes(getResources(),
					getActivity());
			adpReceiptType.NotiDB(arrReceiptType);
		}

	}

	private class ReportUpdateBroadcast extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			Log.e("push-update-report", "1");
			arrReportType = new ArrayList<tbl_Count>();
			arrReportType = DatabaseSupport.getArrReportTypes(getResources(),
					getActivity());
			adpReportType.NotiDB(arrReportType);
		}

	}

	private class TripUpdateBroadcast extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			Log.e("push-update-trip", "1");
			arrTripType = new ArrayList<tbl_Count>();
			arrTripType = DatabaseSupport.getArrTripTypes(getResources(),
					getActivity());
			adpTripType.NotiDB(arrTripType);
		}

	}
}
